package com.app.newiptv.adaper;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.PlayerActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.activity.SplashHomeActivity;
import com.app.newiptv.bean.RecodeCollectBean;
import com.app.newiptv.helper.MySQLiteOpenHelper;
import com.app.newiptv.presenter.PlayDetailsPresenter;
import com.app.newiptv.presenter.SitcomOrderPresenter;
import com.app.newiptv.utils.Paths;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import static com.app.newiptv.utils.Paths.BASEPATH;


/**
 * Created by legend on 2020/5/6.
 */

public class RecodeCollectAdapter extends RecyclerView.Adapter<RecodeCollectAdapter.ViewHolder> {
    private List<RecodeCollectBean> recodeCollectBeanList;
    private Context mContext;
    private Integer operation_type;
//    onInterceptFocusSearch
    public RecodeCollectAdapter(List<RecodeCollectBean> recodeCollectBeanList, Context mContext, Integer operation_type) {
        this.recodeCollectBeanList = recodeCollectBeanList;
        this.mContext = mContext;
        this.operation_type = operation_type;
    }
    public RecodeCollectAdapter(Context context) {
        mContext = context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.recode_collect_item, parent, false);
        return new ViewHolder(item);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final RecodeCollectBean recodeCollectBean = this.recodeCollectBeanList.get(position);
        holder.film_name.setText(recodeCollectBean.getName());
        holder.to_star_tv.setText(recodeCollectBean.getActor());
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
        String time = dateFormat.format(new Date(recodeCollectBean.getPlay_time()));
        holder.paly_time.setText(time);
        holder.item_recode_collect_time_ll.setVisibility(recodeCollectBean.getType() == 0?View.VISIBLE:View.GONE);
        holder.collect_or_recode_title.setText(recodeCollectBean.getType() == 0?"观看时间":"收藏时间");
        holder.collect_time.setText(String.valueOf(recodeCollectBean.getCollect_time()));
        Glide.with(mContext).load(BASEPATH + recodeCollectBean.getPic_url()).error(R.mipmap.ec_entry1).into(holder.img_view);
        holder.item_recode_collect_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        recodeCollectBeanList.remove(recodeCollectBean);
                        MySQLiteOpenHelper sqlHelper = MySQLiteOpenHelper.getInstance(mContext);
                        SQLiteDatabase writeInstance = sqlHelper.getWriteInstance();
                        writeInstance.delete("RecodeCollect", "name="+"'"+ recodeCollectBean.getName() +"'" + " and type=" + operation_type, null);
                        notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setTitle("提示");
                builder.setMessage("确定要删除这条记录吗？");
                builder.show();

            }
        });
        holder.item_recode_collect_paly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer moive_type = recodeCollectBean.getMovie_type();
                final Integer id = recodeCollectBean.getMovie_id();

                switch (moive_type) {
                    case 1001:
                        if (operation_type == 1) {
                            Intent vodIntent = new Intent(mContext, FilmDetailsActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt("id", id);
                            vodIntent.putExtra("bundle", bundle);
                            mContext.startActivity(vodIntent);
                        } else {
                            PlayDetailsPresenter playDetailsPresenter = new PlayDetailsPresenter(mContext);
                            playDetailsPresenter.setDisplayed(true);
                            String url = Paths.HOSTPATH + Paths.CHANNLE_DETAIL + "?ids=" + id;
                            playDetailsPresenter.loadDetailContent(url, new PlayDetailsPresenter.Callback(){
                                @Override
                                public void onError(int code, String msg) {

                                }

                                @Override
                                public void onMovieCompleted(PlayDetailsPresenter presenter, int count) {

                                }

                                @Override
                                public void onDetailCompleted(PlayDetailsPresenter presenter) {
                                    Intent intent = new Intent(mContext, PlayerActivity.class);
                                    intent.putExtra("data", presenter.detailsBean);
                                    intent.putExtra("type", 1001);
                                    intent.putExtra("position",0);
                                    intent.putExtra("play_time", recodeCollectBean.getPlay_time());
                                    mContext.startActivity(intent);
                                }
                            });
                        }

                        break;
                    case 1000:
                        SitcomOrderPresenter sitcomOrderPresenter = new SitcomOrderPresenter(mContext);
                        sitcomOrderPresenter.setDisplayed(true);
                        sitcomOrderPresenter.loadItem(Paths.HOSTPATH + Paths.CHANNLE_DETAIL + "?ids=" + id, new SitcomOrderPresenter.Callback(){
                            @Override
                            public void onError(int code, String msg) {

                            }

                            @Override
                            public void onMovieCompleted(SitcomOrderPresenter presenter, int count) {
//                                int index = presenter.orderPosition * 20 + recodeCollectBean.getSitcom_index();
                                if (operation_type == 1) {
                                    Intent sitcomIntent = new Intent(mContext, SitcomActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("id", id);
                                    sitcomIntent.putExtra("bundle", bundle);
                                    mContext.startActivity(sitcomIntent);
                                } else {
                                    Intent intent = new Intent(mContext, PlayerActivity.class);
                                    intent.putExtra("data", presenter.playDetailsBean);
                                    intent.putExtra("type", 1000);
                                    intent.putExtra("position",0);
                                    intent.putExtra("play_time", recodeCollectBean.getPlay_time());
                                    intent.putExtra("sitcom_index", recodeCollectBean.getSitcom_index());
                                    mContext.startActivity(intent);
                                }

                            }

                            @Override
                            public void onSitcomCompleted(SitcomOrderPresenter presenter, int count) {

                            }

                            @Override
                            public void setImg_view(String url) {

                            }

                            @Override
                            public void setFilm_name(String text) {

                            }

                            @Override
                            public void setDirector_tv(String text) {

                            }

                            @Override
                            public void setTostar_tv(String text) {

                            }

                            @Override
                            public void setYears_tv(String text) {

                            }

                            @Override
                            public void setIntroduce_tv(String text) {

                            }

                            @Override
                            public void setCopyright(String copyright) {

                            }

                            @Override
                            public void setAllCount(String count) {

                            }
                        });
                        break;
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return recodeCollectBeanList.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img_view;
        private TextView film_name;
        private TextView to_star_tv;
        private TextView paly_time;
        private TextView collect_or_recode_title;
        private TextView collect_time;
        private Button item_recode_collect_paly;
        private Button item_recode_collect_delete;
        private LinearLayout item_recode_collect_time_ll;
        public ViewHolder(View itemView) {
            super(itemView);
            img_view = (ImageView) itemView.findViewById(R.id.img_view);
            film_name = (TextView) itemView.findViewById(R.id.film_name);
            to_star_tv = (TextView) itemView.findViewById(R.id.to_star_tv);
            paly_time = (TextView) itemView.findViewById(R.id.paly_time);
            collect_time = (TextView) itemView.findViewById(R.id.collect_time);
            collect_or_recode_title = (TextView) itemView.findViewById(R.id.collect_or_recode_title);
            item_recode_collect_paly = (Button) itemView.findViewById(R.id.item_recode_collect_paly);
            item_recode_collect_delete = (Button) itemView.findViewById(R.id.item_recode_collect_delete);
            item_recode_collect_time_ll = (LinearLayout) itemView.findViewById(R.id.item_recode_collect_time_ll);
        }
    }
}
